package com.xjnt.tvos.utils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

/**
 * Created by caoyong on 17-12-27.
 */

public class NetStateUtils {
    /**
     * 判断是否有网络连接
     * @param context
     * @return
     */
    public static boolean isNetworkConnected(Context context) {
        if (null != context) {
            ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo activeNetwork = connectivityManager.getActiveNetworkInfo();
            if (null != activeNetwork) {
                return activeNetwork.isAvailable();
            }
        }
        return false;
    }

    /**
     * 判断是有线网络
     * @param context
     * @return
     */
    public static boolean isWLan(Context context) {
        int connectedType = getConnectedType(context);
        if (-1 != connectedType) {
            return connectedType == ConnectivityManager.TYPE_ETHERNET;
        }
        return false;
    }

    /**
     * 判断是无线网络
     * @param context
     * @return
     */
    public static boolean isWIFI(Context context) {
        int connectedType = getConnectedType(context);
        if (-1 != connectedType) {
            return connectedType == ConnectivityManager.TYPE_WIFI;
        }
        return false;
    }

    public static boolean isWIFIEnabled(Context context) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
        return (networkInfo != null && networkInfo.getState() == NetworkInfo.State.CONNECTED);
    }

    public static String getHostIp() {
        try {
            for (Enumeration<NetworkInterface> networks = NetworkInterface.getNetworkInterfaces();
                    networks.hasMoreElements();) {
                NetworkInterface network = networks.nextElement();
                for (Enumeration<InetAddress> addresses = network.getInetAddresses();
                        addresses.hasMoreElements();) {
                    InetAddress address = addresses.nextElement();
                    if (!address.isLoopbackAddress()) {
                        return address.getHostAddress();
                    }
                }
            }
        } catch (SocketException ex) {
        } catch (Exception ex) {
        }
        return null;
    }

    /**
     * 获取网络类型
     * @param context
     * @return
     */
    public static int getConnectedType(Context context) {
        if (null != context) {
            ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo activeNetwork = connectivityManager.getActiveNetworkInfo();
            if (null != activeNetwork) {
                if (activeNetwork.isConnected()) {
                    return activeNetwork.getType();
                }
            }
        }
        return -1;
    }
}
